
#include "tagfolder.h"
#include "tf_log.h"
#include "tf_list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined ( __cplusplus)
extern "C"{
#endif
/**
 * Utils to allocate string list;
 * parameters: cnt=> entry number
 * return: pointer to result list;
 *          NULL if failed
 */
string_list* alloc_string_list( size_t cnt ){
    ENTER_FUNC;
    string_list* list= (string_list*) alloc_array_list( cnt);
    LEAVE_FUNC;
    return list;
}

/**
 * To free a string list pointer
 *  But the string_list structure is not freed
 * parameters: list=> target to be freed
 * return: 0=> OK
 *          !0=>Error
 */
int free_string_list( string_list* list){
    ENTER_FUNC;
    int ret = free_array_list(  list);
    LEAVE_FUNC;
    return ret;
}

/**
 * To retrieve a string from target string list
 * parameters: list=> source list
 *          index=> index of the string;
 * return: const pointer to target string;
 *      NULL if errors;
 */
const char* get_from_string_list( string_list* list, size_t index){
    ENTER_FUNC;
    const char* ret= (const char*) get_from_array_list( list, index);
    LEAVE_FUNC;
    return ret;
}

/**
 *  Set the content of specific element of a string list;
 *  parameters: list=> list to be setted
 *          index=> which element to be setted
 *          string=> source content string
 *  return: 0=>OK
 *          !0=>Error
 */
int set_string_list( string_list* list, size_t index, const char* string){
    ENTER_FUNC;
    int ret = -1;
    if( string!=NULL){
        ret= set_array_list(  list, index, string, strlen(string)+1);
    }
    LEAVE_FUNC;
    return ret;
}

string_list* concact_string_list( string_list* lst1, string_list* lst2){
    ENTER_FUNC;
    string_list* ret=concact_array_list( lst1, lst2);
    LEAVE_FUNC;
    return ret;
}

int push_back_string_list( string_list* lst, const char* str){
    ENTER_FUNC;
    int ret=-1;
    if( str!=NULL){
        ret=push_back_array_list( lst, str, strlen(str)+1);
    }
    LEAVE_FUNC;
    return ret;
}

int push_head_string_list( string_list* lst, const char* str){
    ENTER_FUNC;
    int ret=-1;
    if( str!=NULL){
        push_head_array_list( lst, str, strlen(str)+1);
    }
    LEAVE_FUNC;
    return ret;
}

string_list* copy_string_list( string_list* lst){
    ENTER_FUNC;
    string_list* ret=copy_array_list( lst);
    LEAVE_FUNC;
    return ret;
}

/**
 * Tokenize one string and save the result into a string_list
 */
int string_list_tok( const char* str, string_list** lst, const char* tok){
    ENTER_FUNC;
    if( str!=NULL && lst!=NULL && tok!=NULL){
        if( *lst!=NULL){
            free_string_list( *lst);
        }
        char buf[1024];
        (*lst)=alloc_string_list();
        strcpy( buf, str);

        char *p=NULL;//strtok( str, tok);
        p=strtok(buf, tok);
        while( NULL!=p ){
            push_back_string_list( *lst, p); 
            p=strtok( NULL, tok);
        }

        LEAVE_FUNC;
        return 0;
    }
    LEAVE_FUNC;
    return -1;
}

#if defined( __cplusplus)
}   // extern "C"{
#endif
